//Copyright (c) 2010, Goodwine & Tibia ML
//Licensed under new BSD License.

// Adds links to the characters and checks for online status.
var forum = document.getElementsByTagName('body');
forum = forum[0].innerHTML;
var updateCheck = false;
var original;
if(forum.match(/sectionid=.*?">World Boards<\/a>/ig))
{
	world = forum.match(/boardid=.*?">(.*?)<\/a>/);
	world = world[1];
	chrome.extension.sendRequest({'world' : world}, playerListForum);
}
else if(forum.match(/sectionid=.*?">Trade Boards<\/a>/ig))
{
	world = forum.match(/boardid=.*?">(.*?) -.*?<\/a>/);
	world = world[1];
	chrome.extension.sendRequest({'world' : world}, playerListForum);
}
else
	playerListForum("_");
if(opLinkPages.forum)
	chrome.extension.sendRequest({action : 'loadJQuery'}, function(data) {
		if (data != null) {
			eval(data);
			$('td.ff_std font.ff_infotext').each(function() {
				linkCharacter(document.createElement('span'), $(this).parents('td:eq(0)').find('a[href*=?subtopic=characters&name=]:eq(0)').html(), 'left', this.previousSibling);
				this.outerHTML = '<br>' + this.outerHTML;
			});
		} 
		else
			console.warn('Linker: Failed to load jQuery');
	});

// Continuously update each 5 minutes.
function autoUpdateForum()
{
	updateCheck = true;
	chrome.extension.sendRequest({'world' : world}, playerListForum);
}

// Checking for online status
function playerListForum(data)
{
	if (data != null)
	{
		var players = [];
		var pattern =/subtopic=characters&name=.*?>(.*?)<\/A>/img;
		var matches = 0;
		var voc = "?";
		var x;
		var i;
		while((x = pattern.exec(data)) != null)
		{
			players[matches] = x[1].replace(/(\xa0|&#160;|&nbsp;| )/g, ' '); // name
			matches++;
		}
		// Parse the characters.
		var poster = document.getElementsByTagName('tbody');
		if(players.length != 0)
			if(updateCheck == false)
				poster[0].innerHTML += "<td id = 'onlineServer' style='font-size: 7pt; float: right; padding-right: 5px;' nowrap>Online on "+world+": "+players.length+"</td>";
			else
				document.getElementById('onlineServer').innerHTML = "Online on "+world+": "+players.length;
		for(i=1; i < poster.length; i++)
		{
			if(poster[i].innerHTML.match(/<td bgcolor="#505050".*?<b>Author<\/b>/i))
			{
				if(updateCheck == false)
					original = poster[i].innerHTML;
				else
					poster[i].innerHTML = original;
				poster = poster[i].getElementsByTagName('td');
				break;
			}
		}
		var pattern2 = /<a name="post.*?">.*?subtopic=characters&.*?name=.*?>(.*?)</ig;
		// Checks for online status if the character belongs to the server.
		for(i=1; i < poster.length; i++)
		{
			if(x = pattern2.exec(poster[i].innerHTML))
			{
				x[1] = x[1].replace(/(\xa0|&#160;|&nbsp;| )/g, ' ');
				if(players.length != 0 && poster[i].innerHTML.search(world) >= 0)
					putOnOff(poster[i], pattern2, players, x[1]);
			}
		}
		if(players.length != 0)
			setTimeout('autoUpdateForum()', reloadTime(5));
	}
}

// Checks for online status if the character belongs to the server.
function putOnOff(poster, pattern2, players, name)
{
	var x, i;
	var font = poster.getElementsByTagName('font');
	for(var j = 0; j<font.length;j++)
	{
		if(font[j].innerHTML.match(/Inhabitant of .*?<br>Profession:/ig))
		{
			font = font[j];
			break;
		}
	}
	var on = false;
	var stat = document.createElement('span');
	for(var j=0; j<players.length; j++)
	{
		if(name==players[j])
		{
			stat.innerHTML="<b style='color: green; font-size: 11px;'>Online</b>";
			on=true;
		}
	}
	if(!on)
		stat.innerHTML="<b style='color: red; font-size: 11px;'>Offline</b>";
	font.parentElement.insertBefore(stat, font.previousSibling);
}